package com.frank.subject.application.controller;

import com.frank.subject.common.entity.Result;
import com.frank.subject.application.convert.SubjectCategoryDtoConverter;
import com.frank.subject.application.dto.SubjectCategoryDto;
import com.frank.subject.domain.entity.SubjectCategoryBo;
import com.frank.subject.domain.service.SubjectCategoryDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Climb Zou
 * @version v0.0.1
 * @date 2024/1/19 17:27
 */

@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.convertDtoToBo(subjectCategoryDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("adding subject category, category dto: {}", subjectCategoryDTO);
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
            Preconditions.checkArgument(StringUtils.isNotEmpty(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父级ID不能为空");
            subjectCategoryDomainService.insert(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("Adding subject category error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @GetMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory(SubjectCategoryDto subjectCategoryDTO) {
        SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.convertDtoToBo(subjectCategoryDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("Query primary subject category, category dto: {}", subjectCategoryDTO);
            }
            subjectCategoryBO.setParentId(0L);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            return Result.ok(SubjectCategoryDtoConverter.INSTANCE.convertBoListToDtoList(subjectCategoryBoList));
        } catch (Exception e) {
            log.error("Adding subject category error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @GetMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(SubjectCategoryDto categoryDTO) {
        try {
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.convertDtoToBo(categoryDTO);
            if (log.isInfoEnabled()) {
                log.info("Query category by primary, category dto: {}", subjectCategoryBO);
            }
            Preconditions.checkNotNull(categoryDTO.getParentId(), "父级ID不能为空");
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDto> subjectCategoryDtos = SubjectCategoryDtoConverter.INSTANCE.convertBoListToDtoList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtos);
        } catch (Exception e) {
            log.error("Query category by primary error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @PutMapping("/update")
    public Result<Boolean> updateCategory(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.convertDtoToBo(subjectCategoryDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("update category by primary, category dto: {}", subjectCategoryBO);
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "Category ID is not null");
            Boolean result = subjectCategoryDomainService.updateCategoryByPrimary(subjectCategoryBO);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @DeleteMapping("/delete")
    public Result<Boolean> deleteCategory(@RequestBody SubjectCategoryDto subjectCategoryDTO){
        SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.convertDtoToBo(subjectCategoryDTO);
        try {
            if (log.isInfoEnabled()) {
                log.info("Delete category by primary, category dto: {}", subjectCategoryBO);
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "Category ID is not null");
            Boolean result = subjectCategoryDomainService.deleteByPrimary(subjectCategoryBO);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @GetMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDto>> queryCategoryAndLabel(@RequestParam String categoryId) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Query category and label, category id: {}", categoryId);
            }
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategoryAndLabel(categoryId);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConverter.INSTANCE.convertBoListToDtoList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtoList);
        } catch (Exception e) {
            log.error("Query category and label error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }
}
